<?php
	class common extends Mysql{
		function __construct(){
			parent::__construct();
		}
		
		/**
		 * 保存提示
		 * @param $msg
		 * @param $url
		 */
		function submitAlert($msg,$url){
			$alert = "<script>
						alert('".$msg."');
						window.location.href='".$url."';
					</script>";
			echo $alert;
		}
		
		/**
		 * 获得省市区
		 * @param $type
		 * @param $father
		 * @param $selected
		 */
		function getArea($type,$father='',$selected=''){
			$sql="pcc_".$type." where fatherid='".$father."'";
			$result = $this->getList($sql);
			$child = $result['result'];
			if(is_array($child)){
				foreach($child as $k=>$v){
					if($selected==$v[$type.'id']) $child[$k]['selected']=' selected="selected"';
				}
			}
			return $child;
		}
		
		/**
		 * 获得数量
		 * @param $table
		 * @param $where
		 * @param $left
		 * @param $group
		 */
		function getNum($sql,$key="",$group=false){
			//是否有group by
			if(strpos($sql, "group by")){
				$group = true;
			}
			
			if($key!='' && $key!="*"){
				$key = "count(*) as num,".$key;
			}else{
				$key = "count(*) as num";
			}
			$sql="select ".$key." from ".$sql;
			if($group==true) $sql = "select count(*) as num from (".$sql.") as test";
			$query=$this->query($sql);
			$row=$this->fetch_array($query);
			$num=$row['num'];
			return $num;
		}
		
		/**
		 * 获得列表
		 * @param $table
		 * @param $key
		 * @param $where
		 * @param $left
		 * @param $page
		 */
		function getList($sql, $key="*",$num=0,$debug='0'){
			$sqlNum=$sql;
			$sql="select ".$key." from ".$sql;
			if($debug=='1') echo $sql."<br/>";
			if($num!=0){
				$totals=$this->getNum($sqlNum,$key);
				$pagec = new Pages($totals,$num);
				$query=$this->query($sql." limit ".$pagec->limit['a'].",".$pagec->limit['b']);
				$return['page']=$pagec->firstpage().$pagec->prepage().$pagec->more_page().$pagec->nextpage().$pagec->lastpage();
			}else{
				$query=$this->query($sql);
			}
			while($row=$this->fetch_array($query)){
				$result[]=$row;
			}
			$return['result']=$result;
			
			return $return;
		}
		
		/**
		 * 获得内容
		 * @param $table
		 * @param $where
		 * @param $key
		 */
		function getInfo($sql, $key='*',$debug='0'){
			$sql="select ".$key." from ".$sql;
			if($debug=='1') echo $sql."<br/>";
			$query=$this->query($sql);
			$result=$this->fetch_array($query);
			return $result;
		}	

		/**
		 * 删除
		 * @param $table
		 * @param $id
		 * @param $img
		 * @param $dir
		 */
		function del($table, $id, $file='', $search_key="id"){
			if($id!=""){
				$id=substr($id,0,-1);
				
				if(is_array($file)){
					foreach($file as $k=>$v){
						$key = $v['key'];
						$sql = $table." where ".$search_key." in (".$id.")";
						$keys = $this->getList($sql,'',$key);
						$keys = $keys['result'];
						if(is_array($keys)){
							foreach($keys as $key=>$val){
								@unlink($v['dir'].$val[$v['key']]);
							}
						}
					}
				}
				
				$sql="delete from `".$table."` where ".$search_key." in (".$id.")";
				$result = $this->query($sql);
			}else{
				$result = '0';
			}
			return $result;
		}

	
		/**
		 * 将时间戳转换成网站时间
		 * @param unknown_type $date
		 * @return string
		 */
		function getDate($date){
			$now=time();
			$today=date("m月d日");
			$timelag=$now-$date;
			if($timelag<=60){
				$time=$timelag."秒前";
			}elseif($timelag<=3600){
				$time=floor($timelag/60)."分钟前";
			}else{
				$day=date("m月d日",$date);
				$time=date("H:i",$date);
				if($day==$today){
					$time="今天&nbsp;".$time;
				}else{
					if(date("Y",$date)!=date("Y")){
						$time=date("Y年").$day.$time;
					}else{
						$time=$day.$time;
					}
				}
			}
			return $time;
		}
		
		// 浏览器友好的变量输出
		function dump($var, $echo=true, $label=null, $strict=true) {
			$label = ($label === null) ? '' : rtrim($label) . ' ';
			if (!$strict) {
				if (ini_get('html_errors')) {
					$output = print_r($var, true);
					$output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
				} else {
					$output = $label . print_r($var, true);
				}
			} else {
				ob_start();
				var_dump($var);
				$output = ob_get_clean();
				if (!extension_loaded('xdebug')) {
					$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
					$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
				}
			}
			if ($echo) {
				echo($output);
				return null;
			}else
				return $output;
		}

		/**
		 * 二维数组转换成字符串3.0版本
		 * @author --wxy
		 * @param 	array 	$arr
		 * @param	string	$key
		 * @return 	string
		 */
		function convert2($arr,$key='id'){
			$t="";
			if(!empty($arr)){
				foreach($arr as $v){
					$temp[]= $v[$key];
				}
				$temp = array_unique($temp);
				$t = join(",",$temp);
			}
			$t=$t?$t:0;
			return $t;
		}

		/*
		*替换表情（备用）
		*by jsy
		 */
		function ubbReplace($str){ 
		    $str = str_replace(">",'<；',$str); 
		    $str = str_replace(">",'>；',$str); 
		    $str = str_replace("\n",'>；br/>；',$str); 
		    $str = preg_replace("[\[em_([0-9]*)\]]",">img src=\"images/face/$1.gif\" />",$str); 
		    return $str; 
		} 

		
		
	}
?>